package com.seed.application;

import com.mybatisflex.core.audit.AuditManager;
import com.seed.application.gateway.SeedFilter;
import com.seed.application.selector.SeedSelector;
import com.seed.fileud.FileudConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.servlet.Filter;

@Slf4j
@Configuration
@ComponentScan(basePackageClasses = SaConfiguration.class)
@Import({FileudConfiguration.class, SeedSelector.class})
public class SaConfiguration {

    public SaConfiguration() {
        AuditManager.setAuditEnable(true);
        AuditManager.setMessageCollector(auditMessage ->
                log.info("{}, {}ms", auditMessage.getFullSql(), auditMessage.getElapsedTime())
        );
    }

    @Bean
    public FilterRegistrationBean<? extends Filter> getSeedGateway() {
        FilterRegistrationBean<SeedFilter> filter = new FilterRegistrationBean<>();
        filter.setName("seedFilter");
        filter.setFilter(new SeedFilter());
        filter.setOrder(-1);

        return filter;
    }
}
